package com.huishen.edrivenew.net;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Looper;
import android.widget.Toast;

import com.huishen.edrivenew.AppManager;
import com.huishen.edrivenew.Constants;
import com.huishen.edrivenew.util.TLog;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.TextHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayInputStream;

/**
 * 网络回调处理
 * @author wu
 * @version 1.0
 * @created 2015/7/2
 */
public class JsonResponseHandler extends JsonHttpResponseHandler {
    
    
    private Object[] args;
    Context context;

    public JsonResponseHandler(Looper looper, Object... args) {
        this.args = args;
    }

    public JsonResponseHandler(Object... args) {
        this.args = args;
    }


    
    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
        TLog.log("onSuccess code: " + statusCode +  "  " + response.toString());
        
        int code;
        
        try {
            code = response.getInt("code");
            onSuccess(code, response, args);
            
        } catch (JSONException e) {
            onFailure(statusCode, e.getMessage() + response.toString(), args);
        }
        
    }
    
    
    
    
    @Override
    public void onFailure(int statusCode, Header[] headers, String responseString,
            Throwable throwable) {
        TLog.log("onFailure code: " + statusCode + "   " + throwable.getMessage() + "  " + responseString);
        
        if(statusCode == Constants.N_OUTH_ERROR) {
            onSessionFailure();
        } else {
            onFailure(statusCode, responseString, args);
        }
    }
    
    @Override
    public void onFailure(int statusCode, Header[] headers, Throwable throwable,
            JSONArray errorResponse) {
        TLog.log("onFailure code: " + statusCode + "   " + throwable.getMessage());
        onFailure(statusCode, throwable.getMessage(), args);
    }
    @Override
    public void onFailure(int statusCode, Header[] headers, Throwable throwable,
            JSONObject errorResponse) {
        
        TLog.log("onFailure code: " + statusCode + "   " + throwable.getMessage());
        onFailure(statusCode, throwable.getMessage(), args);
    }



    public void onSuccess(int code, JSONObject jsonObj, Object[] args) {
    }

    public void onFailure(int code, String errorMessage, Object[] args) {
    }

    public void onSessionFailure() {
        Toast.makeText(context, "验证失败，请重新登录", Toast.LENGTH_SHORT).show();
        AppManager.getInstance().logout(context);
    }

    
    public void setContext(Context context) {
        this.context = context;
    }
    

}
